package com.itheima.tliasbackend.controller;

import com.itheima.tliasbackend.annotation.SysLog;
import com.itheima.tliasbackend.pojo.Emp;
import com.itheima.tliasbackend.service.EmpService;
import com.itheima.tliasbackend.utils.JwtUtils;
import com.itheima.tliasbackend.utils.Result;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * LoginController
 *
 * @author liliudong
 * @version 1.0
 * @description
 * @date 2023/5/24 10:20
 */
@Slf4j
@RestController
@RequiredArgsConstructor
public class LoginController {

    private final EmpService empService;

    private final String test;

    /**
     * 登录
     *
     * @param emp
     * @return
     */
    @SysLog
    @PostMapping("/login")
    public Result login(@RequestBody Emp emp) {
        Emp empByUsernameAndPassword = empService.login(emp);
        // 下发令牌
        Map<String, Object> claims = new HashMap<>();
        // 放ID
        claims.put("id", empByUsernameAndPassword.getId());
        // 放Name
        claims.put("name", empByUsernameAndPassword.getName());

        String jwt = JwtUtils.generateJwt(claims);
        return Result.success(jwt);
    }
}
